package com.lenovo.server.utils;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import org.hibernate.Query;
import com.lenovo.server.vote.bean.Option;
import com.lenovo.server.vote.bean.Topic;
import com.lenovo.server.vote.bean.VoteUser;

public class DB {
	
	public static Connection getConnection(){
		Properties prop ;
			prop = new Properties() ;
			try {
				prop.load(DB.class.getClassLoader().getResourceAsStream("ProConfig.prop"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		Connection conn = null ;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(prop.getProperty("mysqlURL")) ;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch(SQLException e){
			e.printStackTrace() ;
		} finally {
			//close(conn);
		}
		return conn ;
	}
	
	
	public static Statement createStatement(Connection conn){
		Statement stmt = null ;
		try {
			stmt = conn.createStatement() ;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//close(stmt) ;
		}
		return stmt ;
	}
	
	public static PreparedStatement getPrepareStatement(Connection conn,String sql){
		PreparedStatement pStmt = null ;
		try {
			pStmt = conn.prepareStatement(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//close(pStmt) ;
		}
		return pStmt ;
	}
	
	public static ResultSet getResultSet(Statement stmt,String sql){
		ResultSet rs = null ;
		try {
			rs =  stmt.executeQuery(sql) ;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			//close(rs);
		}
		return rs ;
	}
	
	public static boolean close(Connection conn)
	{
		if(conn != null)
		{
			try {
				conn.close() ;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			conn = null ;
			return true ;
		}
		return false ;
	}
	
	public static boolean close(PreparedStatement pstmt){
		if(pstmt != null){
			try {
				pstmt.close() ;
				pstmt = null ;
				return true ;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false ;
	}
	
	public static boolean close(Statement stmt){
		if(stmt != null)
		{
			try {
				stmt.close() ;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			stmt = null ;
			return true ;
		}
		return false ;
	}
	
	public static boolean close(ResultSet rs){
		if(rs!= null){
			try {
				rs.close() ;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return true ;
		}
		return false ;
	}
	

	public static void executeUpdate(Connection conn , String sql) {
		try {
			Statement stmt = conn.createStatement() ;
			stmt.executeUpdate(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	
	
	public static void main(String[] args){

		
	}
	
}
